package com.nbst.service.base.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nbst.comnutil.LogOut;
import com.nbst.comnutil.NormalResult;
import com.nbst.comnutil.StringUtil;
import com.nbst.comnutil.Tools;
import com.nbst.dao.base.mapper.BaseMapper;
import com.nbst.service.base.IBaseService;

/**
 * Service 基础实现类
 * 
 * @author 兵
 * 
 * @param <T>
 * @param <M>
 */

@Transactional
public abstract class BaseServiceImpl<T, M extends BaseMapper<T>> implements IBaseService<T> {

	@Autowired
	M mapper;

	@Autowired
	LogOut logout;

	private T T;

	public M getMapper() {
		return mapper;
	}

	public LogOut getLogout() {
		return logout;
	}

	@Override
	public NormalResult deleteByPrimaryKey(Object key) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			num = mapper.deleteByPrimaryKey(key);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("删除成功");
			}
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("删除失败");
			result.setDataset(e);
		}
		return result;
	}

	@Override
	public NormalResult insertSelective(T record) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			num = mapper.insertSelective(record);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("新增成功");
			}
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("新增失败");
			result.setDataset(e);
		}
		return result;
	}

	@Override
	public NormalResult findByCondition(Map<String,Object> map, Integer offset, Integer row) {
		NormalResult result = new NormalResult();
		List<T> list = new ArrayList<T>();
		Page page;
		try {
			String orderBy = null;
			if (map != null) {
				if (map.get("orderBy") != null) {
					orderBy = map.get("orderBy").toString();
				}
			}
			if (StringUtil.isEmpty(orderBy)) {
				page = PageHelper.startPage(Tools.getPageNum(offset, row), row);
			} else {
				page = PageHelper.startPage(Tools.getPageNum(offset, row), row, orderBy);
			}
			list = mapper.findByCondition(map);
			if (list.size() == 0) {
				result.setMessage("没有找到数据");
			} else {
				result.setCode("0000");
				result.setMessage("操作成功");
				result.setRows(list);
				result.setTotal(page.getTotal());
			}
		} catch (Exception e) {
			logout.error(e);
		}
		return result;
	}

	@Override
	public NormalResult updateByPrimaryKeySelective(T record) {
		NormalResult result = new NormalResult();
		int num = 0;
		try {
			num = mapper.updateByPrimaryKeySelective(record);
			if (num == 1) {
				result.setCode("0000");
				result.setMessage("修改成功");
			} else {
				result.setCode("9999");
				result.setMessage("修改失败");
			}
		} catch (Exception e) {
			logout.error(e);
			result.setCode("9999");
			result.setMessage("出错了,刷新后再试试吧");
			result.setDataset(e);
		}
		return result;
	}

}
